﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.Revit.RevitExtension;
using YJKArch.Utils;
using YJKControls.Previewer;
using YJKDragCurveJig;

namespace YJKArch.SlopeDim
{
	public partial class DlgSlopeDim : System.Windows.Forms.Form
	{
		public bool InfoChanged
		{
			get
			{
				return this.m_InfoChanged;
			}
			set
			{
				this.m_InfoChanged = value;
			}
		}

		public static DlgSlopeDim GetInstance(ExternalCommandData ecd)
		{
			if (DlgSlopeDim.ded == null || DlgSlopeDim.ded.IsDisposed)
			{
				DlgSlopeDim.ded = new DlgSlopeDim(ecd);
			}
			DlgSlopeDim.ded.RefreshType();
			return DlgSlopeDim.ded;
		}

		public DlgSlopeDim(ExternalCommandData ecd)
		{
			this.revit = ecd;
			this.InitializeComponent();
			this.pictureBox1 = new Previewer(1u);
			this.pictureBox1.Paint += this.pictureBox1_Paint;
			base.Controls.Add(this.pictureBox1);
			this.pictureBox1.Location = new System.Drawing.Point(196, 9);
			this.pictureBox1.Size = new Size(120, 160);
			this.pictureBox1.TabIndex = 7;
			this.pictureBox1.TabStop = false;
			this.RefreshType();
			if (this.cbxArrow.Items.Count > 0)
			{
				this.cbxArrow.SelectedIndex = 0;
			}
		}

		private void RefreshType()
		{
			this.cbxStyle.Items.Clear();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.revit.Application.ActiveUIDocument.Document);
			filteredElementCollector.OfClass(typeof(TextNoteType));
			if (filteredElementCollector != null)
			{
				IEnumerable<Element> enumerable = filteredElementCollector.ToElements();
				new List<Element>();
				foreach (Element element in enumerable)
				{
					this.cbxStyle.Items.Add(new ShowType(element as TextNoteType));
				}
			}
			if (this.cbxStyle.Items.Count > 0)
			{
				this.cbxStyle.SelectedIndex = 0;
			}
			this.textNoteType = ((ShowType)this.cbxStyle.SelectedItem).Type;
		}

		private void pictureBox1_Paint(object sender, PaintEventArgs e)
		{
			Graphics graphics = e.Graphics;
			graphics.Clear(System.Drawing.Color.White);
			this.RefreshPreview(graphics);
			this.angle = Convert.ToDouble(this.txtAngle.Text);
		}

		private void RefreshPreview(Graphics g)
		{
			if (this.InfoChanged)
			{
				this.InfoChanged = false;
				List<TextNoteInfo> list = new List<TextNoteInfo>();
				List<CurveInfo> list2 = new List<CurveInfo>();
				List<FillRegion> list3 = new List<FillRegion>();
				double num = 0.0;
				try
				{
					double num2 = Convert.ToDouble(this.txtAngle.Text) / 180.0 * Math.PI;
					num -= 20.0;
					double num3 = 60.0;
					Line line = YJKLineEx.YJKGetBound(new XYZ(-num3 / 2.0, num, 0.0), new XYZ(num3 / 2.0, num, 0.0));
					XYZ xyz = line.GetEndPoint(0);
					XYZ xyz2 = line.GetEndPoint(1);
					XYZ xyz3 = XYZ.Zero;
					XYZ xyz4 = XYZ.Zero;
					if (this.cbxArrow.SelectedItem.ToString() == "实心箭头" || this.cbxArrow.SelectedItem.ToString() == "空心箭头")
					{
						xyz3 = new XYZ(xyz.X + 5.0, xyz.Y + 2.0, 0.0);
						xyz4 = new XYZ(xyz.X + 5.0, xyz.Y - 2.0, 0.0);
					}
					else
					{
						xyz3 = new XYZ(xyz.X + 5.0, xyz.Y + 2.0, 0.0);
						xyz4 = new XYZ(xyz.X + 5.0, xyz.Y, 0.0);
					}
					Line line2 = YJKLineEx.YJKGetBound(xyz, xyz3);
					Line line3 = YJKLineEx.YJKGetBound(xyz3, xyz4);
					Line line4 = YJKLineEx.YJKGetBound(xyz4, xyz);
					XYZ xyz5 = LineAssistFunc.Midpoint(line);
					xyz = Geometry.RotateTo(xyz, xyz5, XYZ.BasisZ, -num2);
					xyz2 = Geometry.RotateTo(xyz2, xyz5, XYZ.BasisZ, -num2);
					line = YJKLineEx.YJKGetBound(xyz, xyz2);
					line2 = YJKLineEx.YJKGetBound(Geometry.RotateTo(line2.GetEndPoint(0), xyz5, XYZ.BasisZ, -num2), Geometry.RotateTo(line2.GetEndPoint(1), xyz5, XYZ.BasisZ, -num2));
					line3 = YJKLineEx.YJKGetBound(Geometry.RotateTo(line3.GetEndPoint(0), xyz5, XYZ.BasisZ, -num2), Geometry.RotateTo(line3.GetEndPoint(1), xyz5, XYZ.BasisZ, -num2));
					line4 = YJKLineEx.YJKGetBound(Geometry.RotateTo(line4.GetEndPoint(0), xyz5, XYZ.BasisZ, -num2), Geometry.RotateTo(line4.GetEndPoint(1), xyz5, XYZ.BasisZ, -num2));
					CurveInfo item = new CurveInfo(line, PenColor.PC_Green, 1f);
					CurveInfo item2 = new CurveInfo(line2, PenColor.PC_Green, 1f);
					CurveInfo item3 = new CurveInfo(line3, PenColor.PC_Green, 1f);
					CurveInfo item4 = new CurveInfo(line4, PenColor.PC_Green, 1f);
					list2.Add(item);
					list2.Add(item2);
					list2.Add(item3);
					list2.Add(item4);
					if (this.cbxArrow.SelectedItem.ToString() == "实心箭头" || this.cbxArrow.SelectedItem.ToString() == "实心半箭头")
					{
						FillRegion item5 = new FillRegion(new List<Curve>
						{
							line2,
							line3,
							line4
						}, PenColor.PC_Green);
						list3.Add(item5);
					}
					xyz5 = LineAssistFunc.Midpoint(line);
					XYZ xyz6 = (line.GetEndPoint(1) - xyz5).Normalize() * 10.0;
					xyz6 = Geometry.RotateTo(xyz6, Math.PI*.5, XYZ.BasisZ);
					double boundingBoxWidth = (double)(this.cbxValue.Text.Length * 10);
					TextNoteInfo item6 = new TextNoteInfo(this.cbxValue.Text, PenColor.PC_Green, 10.0, xyz5 + xyz6, boundingBoxWidth, 15.0, -num2);
					list.Add(item6);
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message, "盈建科提示");
				}
				this.pictureBox1.TextNotes = list;
				this.pictureBox1.Curves = list2;
				this.pictureBox1.FillRegions = list3;
				this.pictureBox1.ZoomExtents();
			}
		}

		public bool CreateDimData(Autodesk.Revit.DB.View view, ref XYZ ptBase, ref List<JigEdgeInfo> curveInfoList, ref List<JigTextInfo> textInfoList, ref List<CurveLoop> clList)
		{
			double num = (double)view.Scale / 100.0;
			XYZ zero = XYZ.Zero;
			ptBase = zero;
			TextNoteType textNoteType = this.textNoteType;
			double num2 = textNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble();
			double num3 = textNoteType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsDouble();
			byte[] bytes = Encoding.Default.GetBytes(this.cbxValue.Text);
			double num4 = num2 * num3;
			if (num4 < 1.0)
			{
				num4 = 1.0;
			}
			double num5 = (double)bytes.Length * num4 / 1.7 * 3.0 * num;
			double num6 = Convert.ToDouble(this.txtAngle.Text) / 180.0 * Math.PI;
			XYZ startPoint = new XYZ(zero.X, zero.Y, zero.Z);
			XYZ endPoint = new XYZ(zero.X + num5, zero.Y, zero.Z);
			Line line = YJKLineEx.YJKGetBound(startPoint, endPoint);
			double num7 = line.Length / 3.5;
			double num8 = line.Length / 11.0;
			double num9 = line.Length / 6.0;
			XYZ xyz = LineAssistFunc.Midpoint(line);
			XYZ xyz2 = line.GetEndPoint(0);
			XYZ xyz3 = line.GetEndPoint(1);
			XYZ xyz4 = XYZ.Zero;
			XYZ xyz5 = XYZ.Zero;
			if (this.cbxArrow.SelectedItem.ToString() == "实心箭头" || this.cbxArrow.SelectedItem.ToString() == "空心箭头")
			{
				xyz4 = new XYZ(xyz2.X + num7, xyz2.Y + num8, 0.0);
				xyz5 = new XYZ(xyz2.X + num7, xyz2.Y - num8, 0.0);
			}
			else
			{
				xyz4 = new XYZ(xyz2.X + num7, xyz2.Y + num8, 0.0);
				xyz5 = new XYZ(xyz2.X + num7, xyz2.Y, 0.0);
			}
			Line line2 = YJKLineEx.YJKGetBound(xyz2, xyz4);
			Line line3 = YJKLineEx.YJKGetBound(xyz4, xyz5);
			Line line4 = YJKLineEx.YJKGetBound(xyz5, xyz2);
			xyz2 = Geometry.RotateTo(xyz2, xyz, XYZ.BasisZ, -num6);
			xyz3 = Geometry.RotateTo(xyz3, xyz, XYZ.BasisZ, -num6);
			Curve curve = YJKLineEx.YJKGetBound(xyz2, xyz3);
			JigEdgeInfo item = new JigEdgeInfo(xyz2, xyz3, 0.0);
			curveInfoList.Add(item);
			line2 = YJKLineEx.YJKGetBound(Geometry.RotateTo(line2.GetEndPoint(0), xyz, XYZ.BasisZ, -num6), Geometry.RotateTo(line2.GetEndPoint(1), xyz, XYZ.BasisZ, -num6));
			line3 = YJKLineEx.YJKGetBound(Geometry.RotateTo(line3.GetEndPoint(0), xyz, XYZ.BasisZ, -num6), Geometry.RotateTo(line3.GetEndPoint(1), xyz, XYZ.BasisZ, -num6));
			line4 = YJKLineEx.YJKGetBound(Geometry.RotateTo(line4.GetEndPoint(0), xyz, XYZ.BasisZ, -num6), Geometry.RotateTo(line4.GetEndPoint(1), xyz, XYZ.BasisZ, -num6));
			JigEdgeInfo item2 = new JigEdgeInfo(line2.GetEndPoint(0), line2.GetEndPoint(1), 0.0);
			JigEdgeInfo item3 = new JigEdgeInfo(line3.GetEndPoint(0), line3.GetEndPoint(1), 0.0);
			JigEdgeInfo item4 = new JigEdgeInfo(line4.GetEndPoint(0), line4.GetEndPoint(1), 0.0);
			curveInfoList.Add(item2);
			curveInfoList.Add(item3);
			curveInfoList.Add(item4);
			if (this.cbxArrow.SelectedItem.ToString() == "实心箭头" || this.cbxArrow.SelectedItem.ToString() == "实心半箭头")
			{
				CurveLoop curveLoop = new CurveLoop();
				curveLoop.Append(line2);
				curveLoop.Append(line3);
				curveLoop.Append(line4);
				clList.Add(curveLoop);
			}
			XYZ xyz6 = (curve.GetEndPoint(1) - xyz).Normalize() * num9;
			XYZ baseVec;
			XYZ upVec;
			if ((Convert.ToDouble(this.txtAngle.Text) >= 90.0 && Convert.ToDouble(this.txtAngle.Text) <= 180.0) || (Convert.ToDouble(this.txtAngle.Text) >= 180.0 && Convert.ToDouble(this.txtAngle.Text) < 270.0))
			{
				xyz6 = Geometry.RotateTo(xyz6, -Math.PI*.5, XYZ.BasisZ);
				baseVec = Geometry.RotateTo(new XYZ(-1.0, 0.0, 0.0), -num6, XYZ.BasisZ);
				upVec = Geometry.RotateTo(new XYZ(0.0, -1.0, 0.0), -num6, XYZ.BasisZ);
			}
			else
			{
				xyz6 = Geometry.RotateTo(xyz6, Math.PI*.5, XYZ.BasisZ);
				baseVec = Geometry.RotateTo(new XYZ(1.0, 0.0, 0.0), -num6, XYZ.BasisZ);
				upVec = Geometry.RotateTo(new XYZ(0.0, 1.0, 0.0), -num6, XYZ.BasisZ);
			}
			JigTextInfo item5 = new JigTextInfo(textNoteType, xyz + xyz6, baseVec, upVec, (Autodesk.Revit.DB.TextAlignFlags)2176, num5, this.cbxValue.Text);
			textInfoList.Add(item5);
			return true;
		}

		private void SendESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgSlopeDim.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgSlopeDim.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void DlgElevationDim_Load(object sender, EventArgs e)
		{
		}

		private void cbxStyle_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.textNoteType = (this.cbxStyle.SelectedItem as ShowType).Type;
			this.InfoChanged = true;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void DlgElevationDim_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SendESC();
				return;
			}
			base.Focus();
		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.InfoChanged = true;
			this.pictureBox1.Refresh();
		}

		private void comboBox1_TextChanged(object sender, EventArgs e)
		{
			this.InfoChanged = true;
			this.pictureBox1.Refresh();
		}

		private void txtAngle_TextChanged(object sender, EventArgs e)
		{
			if (this.txtAngle.Text == "")
			{
				this.txtAngle.Text = "0";
			}
			this.InfoChanged = true;
			this.pictureBox1.Refresh();
		}

		private void cbxArrow_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.InfoChanged = true;
			this.pictureBox1.Refresh();
		}

		private void DlgSlopeDim_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SendESC();
			base.Hide();
			e.Cancel = true;
		}

		private void txtAngle_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && e.KeyChar != '\r' && e.KeyChar != '\b')
			{
				e.Handled = true;
			}
		}

		private void cbxValue_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && e.KeyChar != '\r' && e.KeyChar != '\b' && e.KeyChar != '%')
			{
				e.Handled = true;
			}
		}

		private ExternalCommandData revit;

		private static DlgSlopeDim ded;

		private TextNoteType textNoteType;

		public double angle;

		private bool m_InfoChanged;

		private Previewer pictureBox1;
	}
}
